package com.bcsq.todolistdemo.util;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 1、当前时间获取
 * 2、SD卡路径获取
 */
public class Util {


    //获取系统时间 视频保存的时间
    public static String getDate() {
        Calendar mCalendar = Calendar.getInstance();
        int year = mCalendar.get(Calendar.YEAR);
        int month = mCalendar.get(Calendar.MONTH);
        int day = mCalendar.get(Calendar.DATE);
        int hour = mCalendar.get(Calendar.HOUR);
        int minute = mCalendar.get(Calendar.MINUTE);
        int second = mCalendar.get(Calendar.SECOND);
        String date = "" + year + (month + 1) + day + hour + minute + second;
        Log.d("date", "date:" + date);
        return date;
    }



    //获取SD卡路径
    public static String getSDPath() {
        File sdDir = null;
        // 判断sd卡是否存在

        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory() ;//获取根目录
            Log.i("chen", "SD卡路径：" + sdDir);
            return sdDir.toString() + File.separator + "DCIM" + File.separator + "video";
        }

        return null;
    }



    /**
     * 判断数字正则表达式
     *
     * @param str
     * @return
     */
    public boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;

    }

    /**
     * 判断字符正则表达式
     *
     * @param str
     * @return
     */
    public boolean isString(String str) {

        return str.matches("[a-zA-Z]+");

    }


    // 对象属性赋值
    public static <T> T gotObjectByObject(Object object, Class<T> clazz) throws Exception {
        T t = null;
        if (clazz != null && object != null) {
            t = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String key = field.getName();
                try {
                    Field field1 = object.getClass().getDeclaredField(key);
                    field1.setAccessible(true);
                    Object val = field1.get(object);
                    field.set(t, val);
                } catch (Exception e) {
                    t = null;
                    System.out.println(object.getClass().getName() + "没有该属性: " + key);
                }
            }
        }
        return t;
    }
}
